<template>
  <div class="dialog-form">
    <el-form ref="platUrlColumnConfigRef" :model="form" :rules="rules" label-width="140px">
      <el-row :gutter="36">
        <el-col :span="12">
          <el-form-item label="父id" prop="pid">
            <el-input v-model="form.pid" placeholder="请输入父id，默认0" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="级数" prop="depth">
            <el-input-number v-model="form.depth" :min="1" :step="1" placeholder="请输入级数,从1开始" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="第三方接口字段名称" prop="thirdKey">
            <el-input v-model="form.thirdKey" placeholder="请输入第三方接口字段名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="第三方接口字段类型" prop="thirdKeyType">
            <el-select v-model="form.thirdKeyType">
              <el-option
                v-for="dict in thirdKeyTypeList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="平台实体字段名称" prop="entityKey">
            <el-input v-model="form.entityKey" placeholder="请输入平台实体字段名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="平台实体类名称" prop="entityClazz">
            <el-input v-model="form.entityClazz" placeholder="请输入平台实体类名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="时间类型格式化格式" prop="timeFormat">
            <el-input v-model="form.timeFormat" placeholder="请输入时间类型格式化格式" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="渠道号" prop="channelCode">
            <el-input v-model="form.channelCode" placeholder="请输入渠道号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="请求方向" prop="direction">
            <el-radio-group v-model="form.direction">
              <el-radio
                v-for="dict in direction"
                :key="dict.value"
                :value="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否用于签名" prop="signFlag">
            <el-radio-group v-model="form.signFlag">
              <el-radio
                v-for="dict in sign_flag"
                :key="dict.value"
                :value="parseInt(dict.value)"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="请求字段的默认值" prop="reqDefaultVal">
            <el-input v-model="form.reqDefaultVal" placeholder="请输入请求字段的默认值" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="响应字段的默认值" prop="respKeyDefaultVal">
            <el-input v-model="form.respKeyDefaultVal" placeholder="请输入响应字段的默认值" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="响应字段标记" prop="respKeyFlag">
            <el-select v-model="form.respKeyFlag">
              <el-option
                v-for="dict in respKeyFlagList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用" prop="enabled">
            <el-radio-group v-model="form.enabled">
              <el-radio
                v-for="dict in sys_normal_disable"
                :key="dict.value"
                :value="parseInt(dict.value)"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="接口字段描述" prop="keyDesc">
            <el-input v-model="form.keyDesc" type="textarea" placeholder="请输入接口字段描述" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onConfirm">确认</el-button>
    </div>
  </div>
</template>

<script setup>
import { listPlatUrlColumnConfig, getPlatUrlColumnConfig, delPlatUrlColumnConfig, addPlatUrlColumnConfig, updatePlatUrlColumnConfig } from "@/api/bus/platUrlColumnConfig"

const { proxy } = getCurrentInstance()
const { direction, sign_flag, sys_normal_disable } = proxy.useDict('direction', 'sign_flag', 'sys_normal_disable')

const emits = defineEmits(['cancel', 'confirm'])

const platUrlColumnConfigRef = ref()
const form = ref({
  id: null,
  pid: null,
  depth: null,
  thirdKey: null,
  thirdKeyType: null,
  keyDesc: null,
  entityKey: null,
  entityClazz: null,
  timeFormat: null,
  channelCode: null,
  direction: null,
  signFlag: 0,
  enabled: 0,
  reqDefaultVal: null,
  respKeyDefaultVal: null,
  respKeyFlag: null,
})
const rules = {
}
const thirdKeyTypeList = [
  { label: 'string', value: 'string' },
  { label: 'number', value: 'number' },
  { label: 'date', value: 'date' },
  { label: 'json', value: 'json' },
  { label: 'jsonArray', value: 'jsonArray' },
]
const respKeyFlagList = [
  { label: '用于正确性校验', value: 0 },
  { label: '作为msg字段', value: 1 },
  { label: '作为data字段', value: 2 }
]

const pageInit = (id) => {
  if (id) {
    getPlatUrlColumnConfig(id).then(response => {
      form.value = response.data
    })
  }
}

const onCancel = () => {
  emits('cancel')
}

const onConfirm = () => {
  platUrlColumnConfigRef.value.validate(valid => {
    if (valid) {
      if (form.value.id) {
        updatePlatUrlColumnConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          platUrlColumnConfigRef.value.resetFields()
          emits('confirm')
        })
      } else {
        addPlatUrlColumnConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          platUrlColumnConfigRef.value.resetFields()
          emits('confirm')
        })
      }
    }
  })
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>